﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;

namespace 西南财经大学学术讲座信息获取小工具
{
    public partial class MainForm : Form
    {
        bool isUserChange = true;
        List<XueShu> xss = new List<XueShu>();
        public MainForm()
        {
            InitializeComponent();
        }
        delegate void UpdateXueShuList();
        delegate void ChangeState(bool state);
        private void UpdateInfo_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(UpdateThread));
            thread.Start();
        }
        private void UpdateThread()
        {
            UpdateXueShuList upXS = delegate()
            {
                if (xss.Count > 0)
                {
                    InfoList.Items.Clear();
                    ListViewItem lv;
                    foreach (XueShu xs in xss)
                    {
                        lv = new ListViewItem(xs.Title);
                        lv.SubItems.Add(xs.Time);
                        InfoList.Items.Add(lv);
                    }
                }
            };
            ChangeState cs = delegate(bool state)
            {
                UpdateInfo.Enabled = state;
                InfoList.Enabled = state;
                MessageLable.Visible = !state;
            };
            UpdateInfo.Invoke(cs, false);
            xss = XueShu.GetXueShu();
            InfoList.Invoke(upXS);
            UpdateInfo.Invoke(cs, true);
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            InfoList.Columns[0].Width = 326;
            InfoList.Columns[1].Width = 186;
            if (WebHelper.isClientWeb())
            {
                this.WindowState = FormWindowState.Normal;
                UpdateInfo_Click(sender, e);
                this.ShowInTaskbar = true;
                notifyIcon1.Dispose();
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
                timer1.Enabled = true;
                notifyIcon1.Visible = true;
            }
            isUserChange = false;
            try
            {
                RegistryKey key = Registry.LocalMachine;
                RegistryKey key2 = key.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (key2.GetValue("学术信息采集系统") == null)
                {
                    RunAsSystem.Checked = false;
                }
                else
                {
                    RunAsSystem.Checked = true;
                }
            }
            catch
            {
                RunAsSystem.Enabled = false;
            }
            finally
            {
                isUserChange = true;
            }
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void InfoList_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < InfoList.Items.Count; i++)
            {
                if (InfoList.Items[i].Selected)
                {
                    Details d = new Details();
                    d.prixs = xss[i];
                    d.ShowDialog();
                    break;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (WebHelper.isClientWeb())
            {
                UpdateInfo_Click(sender, e);
                this.ShowInTaskbar = true;
                timer1.Enabled = false;
                try
                {
                    notifyIcon1.Dispose();
                }
                catch
                {

                }
            }
            else
            {
            }
        }

        private void RunAsSystem_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (isUserChange)
                {
                    if (RunAsSystem.Checked)
                    {
                        RegistryKey key = Registry.LocalMachine;
                        RegistryKey key2 = key.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                        key2.SetValue("学术信息采集系统", Application.ExecutablePath);
                    }
                    else
                    {
                        RegistryKey key = Registry.LocalMachine;
                        RegistryKey key2 = key.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                        key2.DeleteValue("学术信息采集系统");
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("没有足够的权限...你使用的是win7？右键管理员身份运行。\n你的杀毒软件报错？晕哦，那种东西可以相信吗？","出了点问题");
                isUserChange = false;
                RunAsSystem.Checked = !RunAsSystem.Checked;
                isUserChange = true;
            }
            catch (Exception)
            {
                MessageBox.Show("出了点问题...联系我吧\n315@ghy.cn", "错误了");
            }
        }

        private void ExitByContext_Click(object sender, EventArgs e)
        {
            xss.Clear();
            this.Close();
        }

        private void VisitBlogByContext_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://htynkn.blog.163.com");
        }

        private void ViewGPLByContext_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://code.google.com/p/swufe-learning-infomation-tool/");
        }
    }
}
